package com.lagou.edu.controller;

import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("================++++++++++++++跳转登录页面");
        response.sendRedirect(request.getContextPath()+"/login.jsp");
        return null;
    }

    /**
     * 登录
     * @param name
     * @param password
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/loginSystem")
    public ModelAndView userLogin(@RequestParam("username")String name, @RequestParam("password")String password, HttpServletRequest request,HttpServletResponse response) throws IOException {
        if (name.equals("admin") && password.equals("admin")) {
            List resumeList = resumeService.getAllResume();
            // 封装了数据和页面信息的 ModelAndView
            System.out.println("查询所有结果为:" + resumeList);
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("resumeList", resumeList);
            // 视图信息(封装跳转的页面信息) 逻辑视图名
            modelAndView.setViewName("success");
            request.getSession().setAttribute("name",name);
            return modelAndView;
        } else {
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return null;
        }
    }

}
